<?php

namespace app\service;

use app\elastic\ProductElastic;
use app\exception\BusinessException;
use app\model\shop\Product;
use app\model\vipshop\VipProduct;

class ProductService extends BaseService
{
    protected $productElastic;

    public function __construct()
    {
        $this->productElastic = new ProductElastic();
    }

    public function searchProduct($keyword='',$page=1,$size=10)
    {
        $data = [
            'goods_name' => $keyword
        ];
        $product = $this->productElastic->must($data)
                    ->paginate($page,$size)
                    ->search();
        $res = [
            'total' => $product['total'],
            'productList' => $product['data'],
        ];

        return $res;
    }

    public function updateProduct($goods_id,$source)
    {

        $field = 'goods_id,goods_name,category_id,brand_id,goods_type,
               market_price,price,shipping_fee,clicks,sales,evaluates,
               keywords,state,sort,picture,img_id_array,
               description,create_time,update_time,goods_spec_format';

        if($source == 1){
            $product = Product::field($field)
                ->where('goods_id',$goods_id)
                ->append(['doc_id','data_source'])
                ->find();
        }
        if($source == 2){
            $product = VipProduct::field($field)
                ->append(['doc_id','data_source'])
                ->where('goods_id',$goods_id)
                ->find();
        }

        if($product){
            $this->productElastic->singleDoc($product->toArray());
        }
    }





}